#include<bits/stdc++.h>
using namespace std;
int T,m,n,a[1010][1010],b[1010][1010],c[1010][1010];
void read(int &x){
	x=0;
	char c=getchar();
	while(!isdigit(c)&&c!=EOF) c=getchar();
	while(isdigit(c)) x=x*10+c-'0',c=getchar();
}
int main(){
	freopen("matrix.in","r",stdin);
	freopen("matrix.out","w",stdout);
	read(T);
	while(T--){
		int flg=1;
		read(m),read(n);
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
				read(a[i][j]);
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
				read(b[i][j]);
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
				read(c[i][j]);
		if(n<=300){
			for(int i=1;i<=n;i++)
				for(int j=1;j<=n;j++){
					long long now=0;
					for(int k=1;k<=n;k++)
						now+=1ll*a[i][k]*b[k][j]%m,now%=m;
					if(now!=c[i][j]) flg=0;
				}
			if(flg) printf("Yes\n");
			else printf("No\n");
		}else{
			bitset<1010> jl[1010];
			for(int i=1;i<=n;i++)
				for(int j=1;j<=n;j++)
					jl[j][i]=b[i][j]%2;
			for(int i=1;i<=n;i++){
				bitset<1010> now;
				for(int j=1;j<=n;j++)
					now[j]=a[i][j]%2;
				for(int j=1;j<=n;j++){
					bitset<1010> ans=now&jl[j];
					if((int)ans.count()%2!=c[i][j]) flg=0;
				}
			}
			if(flg) printf("Yes\n");
			else printf("No\n");
		}
	}
	return 0;
}
